package com.zzgele.qmzb.api.web;

import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.github.pagehelper.PageInfo;
import com.zzgele.qmzb.api.core.Result;
import com.zzgele.qmzb.api.core.ResultGenerator;
import com.zzgele.qmzb.api.model.FamilyInfo;
import com.zzgele.qmzb.api.model.User;
import com.zzgele.qmzb.api.model.UserParam;
import com.zzgele.qmzb.api.service.CheckAnchorService;
import com.zzgele.qmzb.api.service.FamilyInfoService;
import com.zzgele.qmzb.api.service.UserService;
import com.zzgele.qmzb.api.utils.DateUtil;
import com.zzgele.qmzb.api.utils.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * create by jack
 */
@RequestMapping("/check")
@RestController
public class CheckAnchorController {
    private final Logger logger = LoggerFactory.getLogger(CheckAnchorController.class);
    @DubboConsumer
    private CheckAnchorService checkAnchorService;
    @DubboConsumer
    private FamilyInfoService familyInfoService;
    @DubboConsumer
    private UserService userService;

    @PostMapping("/getFamilyList")
    public Result getFamilyInfoList(Long id, String keyword, Integer page) {
        HashMap map = new HashMap();
        map.put("keyword", keyword);
        map.put("id", id);
        int totalCount=0;
        totalCount=checkAnchorService.getListSize(map);

        Date date1 = new Date();
        String month= DateUtil.dateToStr(date1,"yyyy-MM");//month
        map.put("month", month);
        PageUtil.startPage(page, 20);
        List<HashMap> list = checkAnchorService.getList(map);
        for(HashMap item:list){
            BigDecimal monthTotalTicket= (BigDecimal)item.get("monthTotalTicket");
                if (monthTotalTicket!=null){
                    item.put("monthTotalTicket",monthTotalTicket);
                }else{
                    item.put("monthTotalTicket",0);
                }
            }
//        for(HashMap item:list){
//            Long familyid=(Long) item.get("id");
//            if (familyid!=null){
//                Date date1 = new Date();
//                String month= DateUtil.dateToStr(date1,"yyyy-MM");//month
//                HashMap map1 = new HashMap();
//                map1.put("month", month);
//                map1.put("familyid",familyid);
//                String monthTotalTicket = this.checkAnchorService.getMonthTotalTicket(map1);//本月收入民票
//                if (monthTotalTicket!=null){
//                    item.put("monthTotalTicket",monthTotalTicket);
//                }else{
//                    item.put("monthTotalTicket",0);
//                }
//            }
//        }
        HashMap familyList = PageUtil.getPageInfoHashMap(new PageInfo(list));
        familyList.put("allpages",(totalCount%20)==0?(totalCount/20):(totalCount/20)+1);
        return ResultGenerator.genSuccessResult(familyList);
    }

    /**
     *  根据条件查询主播申请加入家族信息
     * @param login_uid
     * @return
     */
    @PostMapping("/getCheckAnchorStatus")
        public Result getCheckAnchorList(Long login_uid) {
        User user = userService.findById(login_uid);
        if (user == null) {
            return ResultGenerator.genFailResult("用户不存在");
        }
        PageUtil.startPage(1, 1);
        HashMap map = new HashMap();
        map.put("uid",login_uid);
        List<HashMap> list = checkAnchorService.getForFamilyList(map);
        if (list.size()==1){
            return ResultGenerator.genSuccessResult(list);
        }else{
            return ResultGenerator.genFailResult("空");
        }
    }

    /**
     *  提交申请
     * @param login_uid
     *  @param familyid
     * @return
     */
    @PostMapping("/addApply")
    public Result addApply(Long login_uid, Long familyid) {
        User user = userService.findById(login_uid);
        if (user == null) {
            return ResultGenerator.genFailResult("用户不存在");
        }
        FamilyInfo familyInfo = familyInfoService.findById(familyid);
        if (familyInfo == null) {
            return ResultGenerator.genFailResult("家族不存在!");
        }

        Condition condition = new Condition(User.class);
        condition.createCriteria().andCondition("id="+login_uid );
        condition.createCriteria().andCondition(" family_id>0 " );
        List<User> userList = userService.findByCondition(condition);
        if (userList.size()==0){
            return ResultGenerator.genFailResult("主播已经申请过!");
        }else{
            //更新用户信息
            user.setFamilyId(familyid);
            userService.update(user);

            UserParam userParam=new UserParam();
            userParam.setUid(login_uid);
            userParam.setStatus(0);//状态【0=审核中/1=已通过/-2=未通过】
            userParam.setCreateTime(new Date());
            int c=checkAnchorService.save(userParam);
            if (c>0){
                return ResultGenerator.genSuccessResult();
            }else{
                return ResultGenerator.genFailResult("fail!");
            }
        }
    }

}